package com.example.testappwidget.utils

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import com.example.testappwidget.model.BatteryWidgetInfo

/**
 *Author: chinadragon
 *Time: 2024/3/27
 * 监听电池信息工具类
 */
class BatteryListenerUtil(val mContext: Context) {


    private var mBatteryBroadcastReceiver: BatteryBroadcastReceiver? = null
    private var mBatteryStateListener: BatteryStateListener? = null

    /**
     * 注册电池广播
     */
    fun regiester(batteryStateListener: BatteryStateListener?) {
        if (mBatteryBroadcastReceiver == null) {
            mBatteryStateListener = batteryStateListener
            mBatteryBroadcastReceiver = BatteryBroadcastReceiver()
            val filter = IntentFilter()
            filter.addAction(Intent.ACTION_BATTERY_LOW)
            filter.addAction(Intent.ACTION_BATTERY_OKAY)
            filter.addAction(Intent.ACTION_BATTERY_CHANGED)
            filter.addAction(Intent.ACTION_POWER_CONNECTED)
            filter.addAction(Intent.ACTION_POWER_DISCONNECTED)
            mContext.registerReceiver(mBatteryBroadcastReceiver, filter)

            return
        }
        throw Exception("batteryBroadcastReceiver has initialized")
    }

    /**
     * 注销电池广播
     */
    fun unregister() {
        if (mBatteryBroadcastReceiver == null) {
            return
        }

        mContext.unregisterReceiver(mBatteryBroadcastReceiver)
        mBatteryBroadcastReceiver = null
    }

    inner class BatteryBroadcastReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent == null) {
                return
            }

            val tempBatteryWidgetInfo = BatteryWidgetInfo.generate(mContext,intent)
            LogUtil.i(TAG,tempBatteryWidgetInfo.toString())
            mBatteryStateListener?.onChanged(tempBatteryWidgetInfo)

            when (intent.action) {
//                Intent.ACTION_BATTERY_LOW -> {// 低电量
//
//                }
//
//                Intent.ACTION_BATTERY_OKAY -> {// 电量充足
//
//                }

                Intent.ACTION_BATTERY_CHANGED, Intent.ACTION_BATTERY_LOW, Intent.ACTION_BATTERY_OKAY -> {// 电量发生变化
//                    mBatteryStateListener?.onChanged(tempBatteryWidgetInfo)
                }

                Intent.ACTION_POWER_CONNECTED -> {// 接通电源
//                    mBatteryStateListener?.onPowerConnected(tempBatteryWidgetInfo)

                }

                Intent.ACTION_POWER_DISCONNECTED -> {// 不充电
//                    mBatteryStateListener?.onPowerDisconnected(tempBatteryWidgetInfo)
                }
            }


        }

    }

    interface BatteryStateListener {

        /**
         * 电量发生变化
         */
        fun onChanged(batteryWidgetInfo: BatteryWidgetInfo)

        /**
         * 低电量
         */
//        fun onLow(batteryWidgetInfo: BatteryWidgetInfo)

        /**
         * 电量充足
         */
//        fun onOkay(batteryWidgetInfo: BatteryWidgetInfo)

        /**
         * 接通电源
         */
        fun onPowerConnected(batteryWidgetInfo: BatteryWidgetInfo)

        /**
         * 不充电
         */
        fun onPowerDisconnected(batteryWidgetInfo: BatteryWidgetInfo)
    }

    companion object {
        private const val TAG: String = "tag_BatteryListenerUtil"
    }

}